package com.markus.code.exam.yongyou;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/18 14:46
 * todo: 字符串压缩
 */
public class Main {
    public String compress (String str) {
        // write code here
        if (str == null || str.length() == 0){
            return null;
        }
        StringBuilder sb = new StringBuilder();
        char[] strs = str.toCharArray();
        char preCh = strs[0];
        int num = 1;
        for (int i=1;i<strs.length;i++){
            if (preCh == strs[i]){
                num ++ ;
            }else {
                //
                sb.append(preCh).append(num) ;
                preCh = strs[i] ;
                num = 1 ;
            }

        }
        sb.append(preCh).append(num) ;
        String result = sb.toString() ;
        return str.length() < result.length() ? str : result ;
    }


    public static void main(String[] args) {
        String str = "abbccdddaaaa";
        Main demo = new Main();
        System.out.println(demo.compress(str));
    }
}
